﻿
using System.Timers;

namespace Wise.TimeWarrior.OSFunctionalitiesProxy.Impl
{
    /// <summary>
    /// <see cref="ISystemTimer"/>
    /// </summary>
    public class SystemTimer : ISystemTimer
    {
        private Timer t = new Timer();

        /// <summary>
        /// <see cref="ISystemTimer.Interval"/>
        /// </summary>
        public double Interval
        {
            set { t.Interval = value; }
            get { return t.Interval; }
        }

        private ElapsedEventHandler elapsedEvent;

        /// <summary>
        /// <see cref="ISystemTimer.ElapsedTime"/>
        /// </summary>
        public event ElapsedEventHandler ElapsedTime
        {
            add { elapsedEvent += value; }
            remove { if (elapsedEvent != null) elapsedEvent -= value; }
        }


        private void SystemEvents_SessionSwitch(object sender, ElapsedEventArgs e)
        {
            if (elapsedEvent != null)
            {
                elapsedEvent(sender, e);
            }
        }
        /// <summary>
        /// <see cref="ISystemTimer.Start"/>
        /// </summary>
        public void Start()
        {
            t.Elapsed += SystemEvents_SessionSwitch;
            t.Start();
        }


        /// <summary>
        /// <see cref="ISystemTimer.Stop"/>
        /// </summary>
        public void Stop()
        {
            t.Stop();
            t.Elapsed -= SystemEvents_SessionSwitch;
        }
    }
}



